<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息 - 宠物邻里互助平台</title>
    <style>
        :root {
            --primary-color: #FFECB3;  /* 琥珀黄 */
            --secondary-color: #FFD54F; /* 更深的琥珀黄 */
            --light-bg: #FFFDE7;       /* 浅奶油色 */
            --card-bg: #FFF9C4;       /* 淡黄 - 主色调 */
            --text-dark: #5D4037;      /* 深棕色文字 */
            --text-light: #8D6E63;     /* 浅棕色文字 */
            --border-color: #FFE082;   /* 浅黄色边框 */
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
            background-color: var(--light-bg);
            color: var(--text-dark);
            line-height: 1.6;
        }

        .container {
            max-width: 100%;
            margin: 0 auto;
            background-color: white;
            min-height: 100vh;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.05);
        }

        .header {
            padding: 20px;
            text-align: center;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: var(--text-dark);
            font-size: 20px;
            font-weight: 600;
            position: relative;
        }

        .profile-section {
            padding: 25px 20px;
            position: relative;
            margin-top: -20px;
            background: var(--card-bg);
            border-radius: 20px 20px 0 0;
            box-shadow: 0 -5px 15px rgba(0, 0, 0, 0.03);
        }

        .user-card {
            display: flex;
            align-items: center;
            margin-bottom: 25px;
        }

        .user-avatar {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background: linear-gradient(45deg, #FFB74D, #FFCC80);
            margin-right: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 32px;
            font-weight: bold;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }

        .user-details {
            flex: 1;
        }

        .username {
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 5px;
        }

        .user-status {
            font-size: 14px;
            color: #E65100;
            background-color: rgba(255, 183, 77, 0.3);
            padding: 3px 10px;
            border-radius: 20px;
            display: inline-block;
        }

        .stats-container {
            display: flex;
            justify-content: space-between;
            background-color: var(--light-bg);
            border-radius: 15px;
            padding: 15px;
            margin-top: 15px;
            border: 1px solid var(--border-color);
        }

        .stat-item {
            text-align: center;
            flex: 1;
            position: relative;
        }

        .stat-item:not(:last-child)::after {
            content: "";
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            height: 40px;
            width: 1px;
            background-color: var(--border-color);
        }

        .stat-value {
            font-size: 20px;
            font-weight: 700;
            color: #E65100;
        }

        .stat-label {
            font-size: 13px;
            color: var(--text-light);
            margin-top: 5px;
        }

        .menu-section {
            padding: 0 20px;
            margin-top: 10px;
            background-color: white;
        }

        .menu-title {
            font-size: 16px;
            font-weight: 600;
            padding: 15px 0 10px;
            color: var(--text-dark);
            display: flex;
            align-items: center;
        }

        .menu-title::before {
            content: "";
            display: inline-block;
            width: 4px;
            height: 16px;
            background-color: #FFA000;
            margin-right: 10px;
            border-radius: 2px;
        }

        .menu-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 18px 0;
            border-bottom: 1px solid var(--border-color);
            transition: all 0.3s ease;
        }

        .menu-item:active {
            background-color: rgba(255, 204, 128, 0.2);
        }

        .menu-item:last-child {
            border-bottom: none;
        }

        .menu-item-left {
            display: flex;
            align-items: center;
        }

        .menu-icon {
            width: 24px;
            height: 24px;
            margin-right: 15px;
            color: #FF8F00;
        }

        .menu-text {
            font-size: 16px;
        }

        .menu-value {
            font-size: 14px;
            color: var(--text-light);
            margin-right: 5px;
        }

        .menu-arrow {
            color: #FFB74D;
            font-weight: bold;
        }

        .qr-code-placeholder {
            width: 24px;
            height: 24px;
            background-color: var(--border-color);
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        /* 表单样式调整 */
        .form-container {
            padding: 0;
            margin-top: 0;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: var(--text-dark);
        }

        input[type="text"],
        input[type="email"],
        input[type="tel"] {
            width: 100%;
            padding: 12px;
            border: 1px solid var(--border-color);
            border-radius: 8px;
            transition: border-color 0.3s;
            background-color: var(--light-bg);
        }

        input[type="text"]:focus,
        input[type="email"]:focus,
        input[type="tel"]:focus {
            border-color: #FFA000;
            outline: none;
        }

        .btn-submit {
            background-color: #FFA000;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-weight: 500;
            transition: background-color 0.3s;
            width: 100%;
            font-size: 16px;
        }

        .btn-submit:hover {
            background-color: #FF8F00;
        }

        .message {
            padding: 12px;
            margin-bottom: 20px;
            border-radius: 8px;
            font-size: 14px;
        }

        .success {
            background-color: #E8F5E9;
            color: #2E7D32;
            border-left: 4px solid #4CAF50;
        }

        .error {
            background-color: #FFEBEE;
            color: #C62828;
            border-left: 4px solid #F44336;
        }
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }

        .modal-content {
            background-color: white;
            margin: 15% auto;
            padding: 20px;
            border-radius: 10px;
            width: 90%;
            max-width: 400px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            animation: modalopen 0.3s;
        }

        @keyframes modalopen {
            from {opacity: 0; transform: translateY(-50px);}
            to {opacity: 1; transform: translateY(0);}
        }

        .close-btn {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }

        .close-btn:hover {
            color: black;
        }
        /* 导航栏样式 */
        .navbar {
            background-color: white;
            padding: 0 5%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 80px;
            box-shadow: 0 2px 15px rgba(0, 0, 0, 0.05);
            position: sticky;
            top: 0;
            z-index: 1000;
            border-bottom: 1px solid #FFECB3;
        }

        .nav-logo {
            color: #FFB300;
            font-size: 26px;
            font-weight: bold;
            display: flex;
            align-items: center;
        }

        .nav-logo i {
            margin-right: 12px;
            font-size: 30px;
        }

        .nav-links {
            display: flex;
            list-style: none;
        }

        .nav-links li {
            position: relative;
            margin: 0 10px;
        }

        .nav-links a {
            color: #5D4037;
            text-decoration: none;
            font-size: 16px;
            font-weight: 500;
            padding: 10px 18px;
            border-radius: 20px;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            min-width: 100px; /* 统一按钮最小宽度 */
            justify-content: center;
        }

        .nav-links a:hover {
            background-color: #FFF9C4;
            color: #FF8F00;
        }

        .nav-links a i {
            margin-right: 8px;
            color: #FFB300;
        }

        .dropdown {
            position: relative;
            display: inline-block;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            background-color: white;
            min-width: 200px;
            box-shadow: 0 8px 20px rgba(0,0,0,0.08);
            border-radius: 12px;
            z-index: 1;
            top: 100%;
            left: 0;
            opacity: 0;
            transform: translateY(10px);
            transition: all 0.3s ease;
            border: 1px solid #FFECB3;
            padding: 10px 0;
        }

        .dropdown:hover .dropdown-content {
            display: block;
            opacity: 1;
            transform: translateY(0);
        }

        .dropdown-content a {
            color: #5D4037;
            padding: 12px 20px;
            display: block;
            transition: all 0.2s;
            border-radius: 0;
        }

        .dropdown-content a:hover {
            background-color: #FFF9C4;
            color: #FF8F00;
            transform: none;
        }

        .user-avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background-color: #FFF9C4;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
            color: #FF8F00;
            font-weight: bold;
            font-size: 15px;
        }

        .login-btn {
            background-color: #FFF9C4;
            color: #FF8F00 !important;
        }

        .register-btn {
            background-color: #FFB300;
            color: white !important;
        }

        .register-btn:hover {
            background-color: #FF8F00 !important;
        }

        .logout-btn {
            background-color: #FFE0B2;
            color: #E65100 !important;
            border: 1px solid #FFCC80;
        }

        .logout-btn:hover {
            background-color: #FFCC80 !important;
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
</head>
<body>
<!-- 导航栏 -->
<jsp:include page="head.jsp"/>
<div class="container">
    <div class="header">我的信息</div>
    <div class="profile-section">
        <div class="user-card">
            <div class="user-avatar">
                <c:if test="${not empty user.icon}">
                    <img src="${user.icon}" alt="用户头像" style="width: 100%; height: 100%; border-radius: 50%; object-fit: cover;">
                </c:if>
                <c:if test="${empty user.icon}">
                    ${user.username.substring(0, 1)}
                </c:if>
            </div>
            <div class="user-details">
                <div class="username">${user.username}</div>
                <span class="user-status">${user.role == 1 ? '管理员' : '普通用户'}</span>
            </div>
        </div>

        <div class="stats-container">
            <div class="stat-item">
                <div class="stat-value">${not empty user.points ? user.points : '0'}</div>
                <div class="stat-label">积分</div>
            </div>
            <div class="stat-item">
                <div class="stat-value">${not empty user.trust_score ? user.trust_score : '0'}</div>
                <div class="stat-label">信任分</div>
            </div>
            <div class="stat-item">
                <div class="stat-value">0</div>
                <div class="stat-label">关注</div>
            </div>
        </div>
    </div>

    <div class="menu-section">
        <div class="menu-title">账号信息</div>
        <div class="menu-item">
            <div class="menu-item-left">
                <div class="menu-icon">👤</div>
                <span class="menu-text">用户名</span>
            </div>
            <div>
                <span class="menu-value">${user.username}</span>
                <span class="menu-arrow">›</span>
            </div>
        </div>
        <div class="menu-item">
            <div class="menu-item-left">
                <div class="menu-icon">🆔</div>
                <span class="menu-text">用户ID</span>
            </div>
            <div>
                <span class="menu-value">${user.user_id}</span>
                <span class="menu-arrow">›</span>
            </div>
        </div>
    </div>

    <div class="menu-section">
        <div class="menu-title">联系信息</div>
        <div class="menu-item" onclick="openEditModal('phone', '手机号码', '${not empty user.phone ? user.phone : ""}')">
            <div class="menu-item-left">
                <div class="menu-icon">📞</div>
                <span class="menu-text">手机号码</span>
            </div>
            <div>
                <span class="menu-value">${not empty user.phone ? user.phone : '未设置'}</span>
                <span class="menu-arrow">›</span>
            </div>
        </div>
        <div class="menu-item" onclick="openEditModal('email', '电子邮箱', '${not empty user.email ? user.email : ""}')">
            <div class="menu-item-left">
                <div class="menu-icon">✉️</div>
                <span class="menu-text">电子邮箱</span>
            </div>
            <div>
                <span class="menu-value">${not empty user.email ? user.email : '未设置'}</span>
                <span class="menu-arrow">›</span>
            </div>
        </div>
    </div>

    <!-- 模态框 -->
    <div id="editModal" class="modal">
        <div class="modal-content">
            <span class="close-btn" onclick="closeModal()">&times;</span>
            <h2 id="modalTitle">修改信息</h2>

            <c:if test="${not empty param.success}">
                <div class="message success">个人信息更新成功!</div>
            </c:if>

            <c:if test="${not empty error}">
                <div class="message error">${error}</div>
            </c:if>

            <form id="editForm" action="${pageContext.request.contextPath}/user/updateProfile" method="post">
                <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>

                <div class="form-group">
                    <label id="fieldLabel" for="fieldValue">字段名</label>
                    <input type="text" id="fieldValue" name="fieldValue" required>
                </div>

                <input type="hidden" id="fieldName" name="fieldName">

                <div class="form-group">
                    <button type="submit" class="btn-submit">
                        <i class="fas fa-save"></i> 保存更改
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    // 打开模态框
    function openEditModal(fieldName, fieldLabel, currentValue) {
        document.getElementById('modalTitle').textContent = '修改' + fieldLabel;
        document.getElementById('fieldLabel').textContent = fieldLabel;
        document.getElementById('fieldValue').value = currentValue;
        document.getElementById('fieldName').value = fieldName;

        // 根据字段类型设置输入类型
        const inputField = document.getElementById('fieldValue');
        if (fieldName === 'email') {
            inputField.type = 'email';
        } else if (fieldName === 'phone') {
            inputField.type = 'tel';
        } else {
            inputField.type = 'text';
        }

        document.getElementById('editModal').style.display = 'block';
    }

    // 关闭模态框
    function closeModal() {
        document.getElementById('editModal').style.display = 'none';
    }

    // 点击模态框外部关闭
    window.onclick = function(event) {
        const modal = document.getElementById('editModal');
        if (event.target === modal) {
            closeModal();
        }
    }

    // 表单提交处理
    document.getElementById('editForm').addEventListener('submit', function(e) {
        e.preventDefault();

        const fieldName = document.getElementById('fieldName').value;
        const fieldValue = document.getElementById('fieldValue').value;

        // 创建新的表单用于提交
        const submitForm = document.createElement('form');
        submitForm.method = 'post';
        submitForm.action = this.action;

        // 添加CSRF token
        const csrfInput = document.createElement('input');
        csrfInput.type = 'hidden';
        csrfInput.name = '${_csrf.parameterName}';
        csrfInput.value = '${_csrf.token}';
        submitForm.appendChild(csrfInput);

        // 添加当前编辑的字段
        const valueInput = document.createElement('input');
        valueInput.type = 'hidden';
        valueInput.name = fieldName; // 动态字段名（email或phone）
        valueInput.value = fieldValue;
        submitForm.appendChild(valueInput);

        document.body.appendChild(submitForm);
        submitForm.submit();
    });
</script>
</body>
</html>